/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  SupervisionUserAddActivity.java   
 * @Package com.jiyi.activity.supervision   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-10-26 下午4:11:31   
 * @version V1.0     
 */
package com.jiyi.activity.supervision;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.CheckUtil;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: SupervisionUserAddActivity
 * @Description:TODO(监管用户新增)
 * @author: HYM
 * @date: 2017-10-26 下午4:11:31
 * 
 */
public class SupervisionUserAddActivity extends ActivityBase {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.etName)
	EditText etName;
	@BindView(R.id.etAccount)
	EditText etAccount;
	@BindView(R.id.etPassword)
	EditText etPassword;
	@BindView(R.id.etSecPassword)
	EditText etSecPassword;

	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_supervision_user_add);
		ButterKnife.bind(this);
		init();
	}

	private void init() {
		setTop();
		setViewStatus();
	}

	private void setViewStatus() {
		// 设置必填项目验证
		etName.setTag(R.id.tag_view_empty, "名称不能为空");
		etAccount.setTag(R.id.tag_view_empty, "账号不能为空");
		etPassword.setTag(R.id.tag_view_empty, "密码不能为空");
		etSecPassword.setTag(R.id.tag_view_empty, "确认密码不能为空");
	}

	private void setTop() {
		if (UserInfoUtil.getCurrentUserSupervisionRole() == 1) {
			tvTitle.setText("新增参谋");
		} else {
			tvTitle.setText("新增助理");
		}
		tvSearch.setVisibility(View.GONE);
	}

	@OnClick({ R.id.tvBack, R.id.tvAdd })
	public void onClick(View view) {
		Intent intent;
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvAdd:
			if (CheckUtil.checkView(etName, etAccount, etPassword,
					etSecPassword)) {
				if (!etSecPassword.getText().toString()
						.equals(etPassword.getText().toString())) {
					ToastUtil.getInstances().showMsg("两次密码不一致");
					etPassword.requestFocus();
					return;
				}
				doPost();
			}
			break;
		default:
			break;
		}
	}

	void doPost() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("jg_role_id", UserInfoUtil.getCurrentUserSupervisionRole()
				+ "");
		params.put("password", etPassword.getText().toString());
		params.put("name", etName.getText().toString());
		params.put("account", etAccount.getText().toString());

		dialog = showDialog(dialog);

		VolleyRequest.StringRequestPost(this, WebApi.SUPERVISION_USER_ADD, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}
}
